*******************************************************************************
************** Gender and age 1983-1999 *******************************************************************************
*https://px.hagstofa.is/pxen/pxweb/en/Ibuar/Ibuar__mannfjoldi__1_yfirlit__yfirlit_mannfjolda/MAN00101.px/
import delimited "popdata\raw\MAN00101_20240417-090541_kynaldur8321.csv", varnames(2)

rename v3 v1983
rename v7 v1987
rename v11 v1991
rename v15 v1995
rename v19 v1999

keep kyn aldur v1983 v1987 v1991 v1995 v1999

gen male=.
	replace male=1 if kyn=="Karlar"
	replace male=0 if kyn!="Karlar"
	label variable male "Is R male?"
	label define male 1 "Male" 0 "Female/other"
	label values male male
	order male, after(kyn) 
	drop kyn

gen age = subinstr(aldur," ára","",1)	
	replace age="0" if age==" Á 1. ári"
	replace age="1" if age==" 1 árs"
	destring age, replace
	order age, after(male)
	drop aldur

destring v*, replace force
collapse (sum) v*, by(male age)
reshape long v, i(male age) j(year)	
	rename v n

* Need gender composition
preserve 
	keep if inrange(age,18,150)
	collapse (sum) n, by(year male)
	save "temp\gender_1983-1999.dta", replace
restore

* Now we create age categories	
gen agecat=.
	replace agecat=1 if inrange(age,18,29)
	replace agecat=2 if inrange(age,30,49)
	replace agecat=3 if inrange(age,50,64)
	replace agecat=4 if inrange(age,65,150)
	label variable agecat "Age category"
	label define agecat 1 "18-29" 2 "30-49" 3 "50-64" 4 "65+"
	label values agecat agecat 
	order agecat, after(age)

	drop if mi(agecat)
	drop age 

collapse (sum) n, by(year agecat)

save "temp\agecat_1983-1999.dta", replace
	
*******************************************************************************
************** Gender and age 1999-2021, voting eligible population *******************************************************************************
* https://px.hagstofa.is/pxen/pxweb/en/Ibuar/Ibuar__mannfjoldi__3_bakgrunnur__Rikisfang/MAN04103.px/
import delimited "popdata\raw\MAN04103_20240417-094242_rikisfang9921.csv", varnames(2) clear

rename ár year

gen male=.
	replace male=1 if kyn=="Karlar"
	replace male=0 if kyn!="Karlar"
	label variable male "Is R male?"
	label define male 1 "Male" 0 "Female/other"
	label values male male
	order male, after(kyn) 
	drop kyn

* Now we create age categories	

* FIRST WE CUT 15-19 YEAR CAT IN HALF SINCE ONLY 18-19 CAN VOTE
rename mannfjöldieftirríkisfangikynioga n
replace n=round(n/2) if aldur=="15-19 ára"

* Need gender composition
preserve 
	collapse (sum) n, by(year male)
	save "temp\gender_1999-2021.dta", replace
restore

gen agecat=.
	replace agecat=1 if inlist(aldur,"15-19 ára","20-24 ára","25-29 ára")
	replace agecat=2 if inlist(aldur,"30-34 ára","35-39 ára","40-44 ára","45-49 ára")
	replace agecat=3 if inlist(aldur,"50-54 ára","55-59 ára","60-64 ára")
	replace agecat=4 if mi(agecat)
	
	label variable agecat "Age category"
	label define agecat 1 "18-29" 2 "30-49" 3 "50-64" 4 "65+"
	label values agecat agecat 
	order agecat, after(male)
	
collapse (sum) n, by(year agecat)

save "temp\agecat_1999-2021.dta", replace

*******************************************************************************
* Create combined gender targets
*******************************************************************************

use "temp\gender_1983-1999.dta", clear 
gen source="83-99"

append using "temp\gender_1999-2021.dta"

* Do both sources give same composition for 1999?
bys source year (male): egen total_n=total(n)
	gen n_share=n/total_n
	* 83-99 gives male share of 49.6% vs 99-21 with 49.9% --> OK
	drop if year==1999 & source=="83-99"

keeporder year male n 
sort year male

gen xswt=1 
gen _one=1

save "popdata\targets\male_1983-2021.dta", replace


*******************************************************************************
* Create combined age category targets
*******************************************************************************

use "temp\agecat_1983-1999.dta", clear 
gen source="83-99"

append using "temp\agecat_1999-2021.dta"

* Do both sources give same composition for 1999?
bys year source (agecat): egen total_n=total(n)
	gen n_share=n/total_n
	* 83-99 gives 18-29 share of 25.2% vs 99-21 with 25.6% --> OK
	drop if year==1999 & source=="83-99"
	
keeporder year agecat n 
sort year agecat

gen xswt=1 
gen _one=1

save "popdata\targets\agecat_1983-2021.dta", replace


*******************************************************************************
************** Education 
*******************************************************************************

** 1981 
*https://px.hagstofa.is/pxen/pxweb/en/Ibuar/Ibuar__manntal__1981__1_einstaklingar/CEN0016.px/
import delimited "popdata\raw\CEN0016_20240416-114927_menntun81.csv", varnames(2) clear

rename hćstamenntunarstađa menntun
rename alls n
gen year=1981

gen educ = .
	replace educ=1 if menntun=="Grunnskólastig eđa minna"
	replace educ=2 if menntun=="Framhaldsstig"
	replace educ=3 if menntun=="Háskólastig" 
	drop if mi(educ)
	label variable educ "Education"
	label define educ 1 "Primary or less" 2 "Secondary" 3 "Tertiary"
	label values educ educ
	
	keeporder year educ n 	

	* The census data includes all from age 15.
	* Have to substract 15-17 year olds, which we assume are all in educ==1
	* Get data from here: 
	* https://px.hagstofa.is/pxis/pxweb/is/Ibuar/Ibuar__manntal__1_yfirlit/CEN9901.px
	* 15-19 year olds are n=22297. Subtract 3/5 of that, which is 13378
	replace n=n-13378 if educ==1

** 2011-21
* https://px.hagstofa.is/pxen/pxweb/en/Ibuar/Ibuar__manntal__2021__3_menntun/CEN1311.px/	
frame create educ2
frame change educ2 

import delimited "popdata\raw\CEN1311_20240416-115534_menntun1121.csv", varnames(2) clear
	rename menntunarstaða menntun 
	rename innlendir16áraogeldrialls2011 n2011
	rename innlendir16áraogeldrialls2021 n2021
	keep menntun n*
	reshape long n, i(menntun) j(year)

	gen educ = .
		replace educ=1 if menntun=="Grunnmenntun"
		replace educ=2 if menntun=="Framhaldsskólamenntun"
		replace educ=3 if menntun=="Háskólamenntun" 
		label variable educ "Education"
		label define educ 1 "Primary or less" 2 "Secondary" 3 "Tertiary"
		label values educ educ

	* The census data includes all from age 16.
	* Have to substract 16-17 year olds, which we assume are all in educ==1
	* Same source as above
	* 2011: 15-19 year olds are n=23092. Subtract 2/5 of that, which is 9236
	* 2021: 15-19 year olds are n=22045. Subtract 2/5 of that, which is 8818
	replace n=n-9236 if educ==1 & year==2011
	replace n=n-8818 if educ==1 & year==2021
	
	keeporder year educ n 	
frame change default	

frameappend educ2

* Now we interpolate share of electorate by education level 
bys year (educ): egen total_n=total(n)
	gen n_share=n/total_n

tsset educ year
tsfill
bys educ: ipolate n_share year, gen(n_shareI)
bys educ: ipolate total_n year, gen(total_nI)

drop n
gen n = n_shareI*total_nI

keeporder year educ n 
keep if inlist(year,1983,1987,1991,1995,1999,2003,2007,2009,2013,2016,2017,2021)
replace n = round(n)
sort year educ

gen xswt=1 
gen _one=1

save "popdata\targets\educ_1983-2021.dta", replace

*******************************************************************************
************** Voting behavior
*******************************************************************************
* https://px.hagstofa.is/pxen/pxweb/en/Ibuar/Ibuar__kosningar__althingi__althurslit/KOS02121.px/
import delimited "popdata\raw\KOS02121_20240417-112807_flokkur8321.csv", varnames(2) clear

rename úrslitalþingiskosninga19632021 n
rename ár year 
keeporder flokkur year n
destring n, force replace

gen party=. 
	replace party=1 if inlist(flokkur,"Alþýðuflokkur","Samfylkingin")
	replace party=2 if flokkur=="Framsóknarflokkur"
	replace party=3 if flokkur=="Sjálfstæðisflokkur"
	replace party=4 if inlist(flokkur,"Alþýðubandalag","Vinstrihreyfingin - grænt framboð")
	replace party=5 if flokkur=="Frjálslyndi flokkurinn"
	replace party=6 if flokkur=="Samtök um kvennalista"
	replace party=7 if flokkur=="Bandalag jafnaðarmanna"
	replace party=10 if flokkur=="Borgaraflokkur"
	replace party=11 if flokkur=="Þjóðvaki, hreyfing fólksins"
	replace party=12 if flokkur=="Borgarahreyfingin"
	replace party=39 if flokkur=="Björt framtíð"
	replace party=40 if flokkur=="Píratar"
	replace party=50 if flokkur=="Viðreisn"
	replace party=51 if flokkur=="Flokkur fólksins"
	replace party=52 if flokkur=="Miðflokkurinn"
	replace party=89 if mi(party)
	
	* Now we add parties that received under 5% to to other 
	replace party=89 if party==7 & year==1987
	replace party=89 if party==5 & inlist(year,1999,2009)
	replace party=89 if party==39 & year==2017

* Add turnout data	
frame create turnout 
frame change turnout 
	import delimited "popdata\raw\turnout_8321.csv", clear
	gen n=n_voters-n_voted
	gen party=0 
	drop if mi(year)
	keeporder year party n
frame change default 
frameappend turnout

keep if !mi(n)

label define parties ///
	1 "Alþýðuflokkur/Samfylking" ///
	2 "Framsóknarflokkur" ///
	3 "Sjálfstæðisflokkur" ///
	4 "Alþýðubandalag/VG" ///
	5 "Frjálslyndir" ///
	6 "Kvennalistinn" ///
	7 "Bandalag jafnaðarmanna" ///
	10 "Borgaraflokkurinn" ///
	11 "Þjóðvaki" ///
	12 "Borgarahreyfingin" ///
	39 "Björt framtíð" ///
	40 "Píratar" ///
	50 "Viðreisn" ///
	51 "Flokkur fólksins" ///
	52 "Miðflokkurinn" ///
	89 "Aðrir" ///
	0 "Kaus ekki"
label values party parties
label save parties using "temp/parties", replace


keeporder year party n
sort year party
	drop if year==1979
	collapse (sum) n, by(year party)

gen xswt=1 
gen _one=1
	
save "popdata\targets\party_1983-2021.dta", replace
	
*******************************************************************************
************** Party voters in each district 
*******************************************************************************
*https://px.hagstofa.is/pxen/pxweb/en/Ibuar/Ibuar__kosningar__althingi__althurslit/KOS02124.px/
import delimited "popdata\raw\KOS02124_20240417-120707_kjordaemi8399.csv", varnames(2) clear
rename  kosningaúrslitíalþingiskosningum n

gen year=.
	replace year=1983 if ár=="23. apríl 1983"
	replace year=1987 if ár=="25. apríl 1987"
	replace year=1991 if ár=="20.apr.91"
	replace year=1995 if ár=="8. apríl 1995"
	replace year=1999 if ár=="8. maí 1999"

gen district = .
	replace district=11 if kjördæmi=="Reykjavíkurkjördæmi"
	replace district=12 if kjördæmi=="Reykjaneskjördæmi"
	replace district=13 if kjördæmi=="Vesturlandskjördæmi"
	replace district=14 if kjördæmi=="Vestfjarðarkjördæmi"
	replace district=15 if kjördæmi=="Norðurlandskjördæmi vestra"
	replace district=16 if kjördæmi=="Norðurlandskjördæmi eystra"
	replace district=17 if kjördæmi=="Austurlandskjördæmi"
	replace district=18 if kjördæmi=="Suðurlandskjördæmi"

label define districts ///
	 1 "Norðausturkjördæmi" ///
	 2 "Norðvesturkjördæmi" ///
	 3 "Reykjavíkurkjördæmi Norður" ///
	 4 "Reykjavíkurkjördæmi Suður" ///
	 5 "Suðurkjördæmi" ///
	 6 "Suðvesturkjördæmi" ///
	11 "Reykjavíkurkjördæmi" ///
	12 "Reykjaneskjördæmi" ///
	13 "Vesturlandskjördæmi" ///
	14 "Vestfjarðarkjördæmi" ///
	15 "Norðurlandskjördæmi vestra" ///
	16 "Norðurlandskjördæmi eystra" ///
	17 "Austurlandskjördæmi" ///
	18 "Suðurlandskjördæmi"
label values district districts
label save districts using "temp/districts", replace
	
keeporder year district n
	
save "temp\districts_partyvoters_1983-1999.dta", replace
* https://px.hagstofa.is/pxen/pxweb/en/Ibuar/Ibuar__kosningar__althingi__althurslit/KOS02122.px/
import delimited "popdata\raw\KOS02122_20240417-121227_kjordaemi0309.csv", varnames(2) clear	
do "temp/districts"

gen district=. 
	replace district=1 if kjördæmi=="Norðausturkjördæmi"
	replace district=2 if kjördæmi=="Norðvesturkjördæmi"
	replace district=3 if kjördæmi=="Reykjavíkurkjördæmi norður"
	replace district=4 if kjördæmi=="Reykjavíkurkjördæmi suður"
	replace district=5 if kjördæmi=="Suðurkjördæmi"
	replace district=6 if kjördæmi=="Suðvesturkjördæmi"
label values district districts

rename ár year 
rename alls n
keeporder year district n
save "temp\districts_partyvoters_2003-2009.dta", replace

* https://px.hagstofa.is/pxen/pxweb/en/Ibuar/Ibuar__kosningar__althingi__althkjosendur/KOS02101.px/
use "popdata\raw\KOS02101_kjordaemiflokkur1321.dta", clear

do "temp/districts"
label values district districts	

drop if vote==0
collapse (sum) n_voted, by(year district)
rename n_voted n

append using "temp\districts_partyvoters_2003-2009.dta"
append using "temp\districts_partyvoters_1983-1999.dta"

sort year district 

gen xswt=1 
gen _one=1


save "popdata\targets\district_1983-2021.dta", replace


